<template>
  <div class="list-container">
    <div class="search-wrap clearfix">
      <div class="search-wrap-conditions fl marginL10">
        <el-select
          v-model="listQuery.condition.status"
          clearable
          placeholder="请选择状态"
          @change="resetPage"
        >
          <el-option
            v-for="item in optionsStatus"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </div>
      <div class="search-wrap-conditions fl marginL10">
        <el-select
          v-model="listQuery.condition.creatorType"
          clearable
          placeholder="请选择发起者类型"
          @change="resetPage"
        >
          <el-option
            v-for="(item, index) in optionsInitiator"
            :key="index"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </div>
      <div class="search-wrap-conditions fl marginL10">
        <el-input
          v-model="listQuery.condition.keyword"
          class="input-width"
          placeholder="关键词, 标题、发起者"
          @keyup.enter.native="resetPage"
        />
      </div>
      <div class="search-wrap-conditions fl marginL10">
        <div class="block">
          <el-date-picker
            v-model="timer"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="yyyy-MM-dd HH:mm:ss"
            @change="resetPage"
          />
        </div>
      </div>
      <div class="search-wrap-btn fr">
        <el-button :disabled="loading" @click="resetSearch">重置</el-button>
        <el-button type="primary" @click="resetPage">搜索</el-button>
        <!-- <el-button :disabled="loading" type="success" @click="add">添加</el-button> -->
      </div>
    </div>
    <div class="main-wrapper">
      <custom-table :data="listData" :columns="columns">
        <template slot-scope="{ row }">
          <keep-alive>
            <el-button type="text" @click="getDetail(row.id)">查看详情</el-button>
          </keep-alive>
        </template>
      </custom-table>
      <pagination :total="total" :page="listQuery.pager.page" @pagination="pagination" />
    </div>
  </div>
</template>

<script>
import CustomTable from '@/components/CustomTable/index';
import Pagination from '@/components/Pagination/index';
import { getUsersList } from '@/api/users';
import { getTeachingResearchList } from '@/api/activity';
import { teachingStatus, teachingInitiator } from './mock';
import { getTimes } from '@/utils/time';
export default {
  name: 'List',
  components: {
    CustomTable,
    Pagination
  },
  data () {
    return {
      loading: false,
      listQuery: {
        condition: {
          creatorType: null,
          endTime: null,
          keyword: null,
          startTime: null,
          status: null
        },
        pager: {
          page: 1,
          pageSize: 10
        }
      },
      optionsInitiator: teachingInitiator,
      listData: [],
      total: 0,
      timer: [],
      optionsStatus: teachingStatus,
      columns: [
        { label: '序号', value: 'index', align: 'center', width: '80px' },
        { label: '标题', value: 'title', align: 'center' },
        { label: '状态', value: 'statusTxt', align: 'center', width: '120px' },
        { label: '发起者', value: 'creatorName', align: 'center', width: '160px' },
        { label: '时间', value: 'createTime', align: 'center', width: '140px' },
        { label: '操作', value: 'todo', align: 'center', width: '120px' }
      ]
    };
  },
  created () {
    this.init();
  },
  methods: {
    add () { },
    resetPage () {
      this.listQuery.pager.page = 1;
      this.listQuery.pager.pageSize = 10;
      this.init();
    },
    init () {
      this.listQuery.condition.startTime = this.timer && this.timer.length > 0 ? this.timer[0] : null;
      this.listQuery.condition.endTime = this.timer && this.timer.length > 0 ? this.timer[1] : null;
      this.loading = true;
      this.listQuery.condition.keyword = this.listQuery.condition.keyword ? this.listQuery.condition.keyword : null;
      getTeachingResearchList(this.listQuery)
        .then(res => {
          if (this.checkArr(res.data.records)) {
            res.data.records.map(item => {
              switch (item.status) {
                case 'NOT_START':
                  item.statusTxt = '未开始';
                  break;
                case 'STARTED':
                  item.statusTxt = '已开始';
                  break;
                case 'ENDED':
                  item.statusTxt = '已结束';
                  break;
                case 'UNAUDITED':
                  item.statusTxt = '未审核';
                  break;
                case 'NORMAL':
                  item.statusTxt = '正常';
                  break;
                case 'VIOLATE':
                  item.statusTxt = '违规';
                  break;
              }
            });
          }

          this.listData = res.data.records;
          this.total = res.data.total;
          this.loading = false;
        })
        .catch(error => {
          this.loading = false;
        });
      // this.loading = true;
      // getUsersList(this.listQuery).then(res => {
      //   this.listData = res.records;
      //   this.total = res.total;
      //   this.loading = res.loading;
      // }).catch(error => {
      //   this.loading = false;
      // });
    },
    resetSearch () {
      this.listQuery = {
        condition: {
          keyword: null
        },
        pager: {
          page: 1,
          pageSize: 10
        }
      };
      this.timer = [];
      this.init();
    },
    pagination ({ page, limit }) {
      this.listQuery.pager.page = page;
      this.listQuery.pager.pageSize = limit;
      this.init();
    },
    getDetail (id) {
      // this.$router.push({
      //   path: '/activity/teachingDetail',
      //   query: { param: id }
      // });
      const routeData = this.$router.resolve({ path: '/activity/teachingDetail', query: { param: id } });
      window.open(routeData.href, '_blank');
    }
  }
};
</script>

<style lang="scss" scoped></style>
